package ru.papergames.battleserver.network.clientpackets.webserver;

import org.apache.log4j.Logger;
import org.mmocore.network.ReceivablePacket;
import ru.papergames.Config;
import ru.papergames.battleserver.network.WebServerClient;
import ru.papergames.battleserver.network.serverpackets.webserver.WebServerServerPacket;

/**
 * @author: atroshanin
 * Date: 18.04.2010
 * Time: 21:32:52
 */
public abstract class WebServerClientPacket extends ReceivablePacket<WebServerClient> implements WebServerClientPacketTypes {
    protected static final Logger logger = Logger.getLogger(WebServerClientPacket.class.getName());

    @Override
    protected boolean read() {
        try {
            readImpl();
            return true;
        }
        catch (Exception e) {
            logger.fatal("Client: " + getClient().toString() + " - Failed reading: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
            e.printStackTrace();
        }
        return false;
    }

    protected abstract void readImpl();

    @Override
    public void run() {
        try {
            // flood protection
            runImpl();

        }
        catch (Throwable t) {
            t.printStackTrace();
            logger.fatal("Client: " + getClient().toString() + " - Failed running: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
        }
    }

    protected abstract void runImpl();

    protected final void sendPacket(WebServerServerPacket gsp) {
        getClient().sendPacket(gsp);
    }

    /**
     * @return A String with this packet name for debuging purposes
     */
    public abstract String getType();

    /**
     * Overriden with true value on some packets that should disable spawn protection
     */
    protected boolean triggersOnActionRequest() {
        return false;
    }
}
